from django import forms
from django.core.exceptions import ValidationError
from qcloud_cos import CosServiceError

from web.forms.bootstrap import BootStripForm
from web import models
from utils.tencent.cos import check_file


class FolderModelForm(BootStripForm, forms.ModelForm):
    class Meta:
        model = models.FileRepository
        fields = ['name', ]

    def __init__(self, request, parent_object, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request
        self.parent_object = parent_object

    def clean_name(self):
        name = self.cleaned_data['name']
        queryset = models.FileRepository.objects.filter(name=name, file_type=2, project=self.request.tracer.project)
        if self.parent_object:
            exists = queryset.filter(parent=self.parent_object).exists()
        else:
            exists = queryset.filter(parent__isnull=True).exists()
        if exists:
            raise ValidationError('文件夹已存在')
        return name


class FileModelForm(forms.ModelForm):
    etag = forms.CharField(label='ETag')

    class Meta:
        model = models.FileRepository
        exclude = ['project', 'file_type', 'update_user', 'update_datetime']

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request

    def clean_file_path(self):
        return 'https://{}'.format(self.cleaned_data['file_path'])

    # def clean(self):
    #     key = self.cleaned_data.get('key')
    #     etag = self.cleaned_data.get('etag')
    #     size = self.cleaned_data.get('size')
    #     if not key or not etag:
    #         return self.cleaned_data
    #     # 向后台cos校验是否合法
    #     # SDK调用api
    #     try:
    #         result = check_file(self.request.tracer.project.bucket, self.request.tracer.project.region, key)
    #     except CosServiceError as e:
    #         self.add_error(key, '文件未上传成功')
    #         return self.cleaned_data
    #     cos_etag = result.get('etag')
    #     if cos_etag != etag:
    #         self.add_error('etag', 'ETag错误')
    #     cos_length = result.get('Content-Length')
    #     if int(cos_length) != size:
    #         self.add_error('size', '文件大小错误')
    #
    #     return self.cleaned_data
